package io.renren.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.MapUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.app.dao.BiaodiwuDao;
import io.renren.modules.app.dao.CategoryDao;
import io.renren.modules.app.entity.BiaodiwuEntity;
import io.renren.modules.app.entity.CategoryEntity;
import io.renren.modules.app.service.BiaodiwuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("biaodiwuService")
public class BiaodiwuServiceImpl extends ServiceImpl<BiaodiwuDao, BiaodiwuEntity> implements BiaodiwuService {

    @Autowired(required = false)
    private CategoryDao categoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BiaodiwuEntity> page = this.page(
                new Query<BiaodiwuEntity>().getPage(params),
                new QueryWrapper<BiaodiwuEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<BiaodiwuEntity>  get12BiaodiwuByCategory(String categoryName){
        MapUtils page = new MapUtils().put("page", 1).put("limit", 12);

        QueryWrapper<BiaodiwuEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("show_index",1);
        queryWrapper.eq("category_name",categoryName);

        PageUtils pageUtils = new PageUtils(this.page(
                new Query<BiaodiwuEntity>().getPage(page), queryWrapper
        ));
        List<BiaodiwuEntity> list = (List<BiaodiwuEntity>) pageUtils.getList();
        return list;
    }

    @Override
    public PageUtils queryPageByCategoryId(Map<String, Object> params,String categoryId) {
        List<CategoryEntity> categoryEntities = categoryDao.selectCategorysByParentId(categoryId);
        List<String> cids = new ArrayList<>();
        for (CategoryEntity categoryEntity : categoryEntities) {
            cids.add(categoryEntity.getId());
        }
        QueryWrapper<BiaodiwuEntity> queryWrapper = new QueryWrapper<>();
        if(cids.size()>0){
            queryWrapper.in("category_id",cids);
        }
        return new PageUtils(this.page(
                new Query<BiaodiwuEntity>().getPage(params),queryWrapper

        ));
    }

}
